﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Hr.IBLL;
using Newtonsoft.Json;
using Hr.BLLContainer;
using System.Data.Entity;
using Newtonsoft.Json.Linq;
using Hr.BLL;
using Hr.Model;
using System.Web;
using HR.HrHelper;
using Microsoft.Ajax.Utilities;
using System.Web.Helpers;

namespace HR.Areas.liujunjie.Controllers.Article
{
    public class ArticleMangeController : ApiController
    {
        //导入BLL层
        protected ArticleService Article = new ArticleService();

        int code = 0;
        string msg = "success";

        #region 新闻管理页面数据

        /// <summary>
        /// 新闻管理页面数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新闻管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>
        [HttpGet]
        public JObject ArticleList(int page, int limit)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Article.ArticleList(page,limit) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        #endregion

        #region 根据Title查询新闻管理页面数据

        /// <summary>
        /// 根据Title查询新闻管理页面数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新闻管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>
        [HttpGet]
        public JObject TitleSelectArticleList(int page, int limit, string Title)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Article.TitleSelectArticleList(page,limit,Title) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        #endregion

        #region 根据pk_Article删除新闻管理页面数据

        /// <summary>
        /// 根据pk_Article删除新闻管理页面数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新闻管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>
        [HttpGet]
        public JObject DelArticleList(int pk_Article)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",1 },
                {"data",Article.DelArticleList(pk_Article) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        #endregion

        #region 类型和状态下拉框
        /// <summary>
        /// 下拉框绑定
        /// </summary>
        /// <param name="id">类型和状态下拉框</param>
        /// <returns>修改和增加页面里的下拉框</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject SelectBinding()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg","1" },
                {"TypeIDdata",Article.SelectTypeBinding() },
                {"StatusIDdata",Article.SelectStatusBinding() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 新闻查看 包括记录点击数
        /// <summary>
        /// 新闻查看 包括记录点击数
        /// </summary>
        /// <param name="id">pk_Article</param>
        /// <returns>新闻查看</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/24</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject ArticleDataBinding(int pk_Article)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg","1" },
                {"data",Article.ArticleDataBinding(pk_Article) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        #endregion


        #region 新闻管理修改添加
        /// <summary>
        /// 新闻管理修改添加
        /// </summary>
        /// <param name="id">pk_Article</param>
        /// <returns>修改和增加页面</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/22</create>
        /// <updata>null</updata>
        [HttpGet]
        public JObject GetModels(int pk_Article)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Article.GetModels(pk_Article)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        [System.Web.Mvc.ValidateInput(false)]
        [HttpPost]
        public JObject EditArticleList()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            //调用jsonhelper帮助类转换成对象
            Article_List al = JsonHelper.DeserializeJsonToObject<Article_List>(data["Article"]);

            string type = data["Type"];
            //string NewsContent = data["NewsContent"];//这个可以不用单独传
            int CreateUser = int.Parse(data["CreateUser"]);

            if (type.Equals("Updata"))
            {
                Article_List SelTl = Article.GetModels(al.pk_Article);
                msg = "更新成功";
                if (SelTl == null)
                {
                    code = 1;
                    msg = "新闻不存在";
                }
                else
                {
                    if (!Article.EditArticleList(al, type, CreateUser))
                    {
                        code = 1;
                        msg = "更新失败";
                    }
                }
            }
            else
            {
                msg = "添加成功";

                if (!Article.EditArticleList(al, type, CreateUser))
                {
                    code = 1;
                    msg = "添加失败";
                }

            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Article.GetModels(al.pk_Article)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion
    }
}
